package advancedFeatures;

import java.util.*;
import java.util.function.*;

/**
 * Lambda 表达式与函数式接口示例
 */
public class LambdaAndFunctionalInterfaces {
    public static void main(String[] args) {
        // 1. 基本 Lambda 表达式示例
        System.out.println("=== 基本 Lambda 表达式示例 ===");
        Runnable runnable = () -> System.out.println("Hello Lambda!");
        runnable.run();

        // 2. Consumer 接口示例
        System.out.println("\n=== Consumer 接口示例 ===");
        Consumer<String> printUpperCase = str -> System.out.println(str.toUpperCase());
        printUpperCase.accept("hello world");

        // 3. Supplier 接口示例
        System.out.println("\n=== Supplier 接口示例 ===");
        Supplier<String> randomString = () -> UUID.randomUUID().toString();
        System.out.println("随机字符串: " + randomString.get());

        // 4. Function 接口示例
        System.out.println("\n=== Function 接口示例 ===");
        Function<String, Integer> stringLength = String::length;
        System.out.println("字符串长度: " + stringLength.apply("Java"));

        // 5. Predicate 接口示例
        System.out.println("\n=== Predicate 接口示例 ===");
        Predicate<String> isLong = str -> str.length() > 5;
        System.out.println("字符串是否超过5个字符: " + isLong.test("Hello World"));

        // 6. BiFunction 接口示例
        System.out.println("\n=== BiFunction 接口示例 ===");
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
        System.out.println("两数之和: " + add.apply(5, 3));

        // 7. 方法引用示例
        System.out.println("\n=== 方法引用示例 ===");
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
        names.forEach(System.out::println);

        // 8. Stream API 与 Lambda 结合示例
        System.out.println("\n=== Stream API 与 Lambda 结合示例 ===");
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        
        // 过滤偶数并计算平方
        numbers.stream()
               .filter(n -> n % 2 == 0)
               .map(n -> n * n)
               .forEach(System.out::println);

        // 9. 自定义函数式接口示例
        System.out.println("\n=== 自定义函数式接口示例 ===");
        Calculator calculator = (a, b) -> a * b;
        System.out.println("自定义计算器结果: " + calculator.calculate(5, 3));
    }

    // 自定义函数式接口
    @FunctionalInterface
    interface Calculator {
        int calculate(int a, int b);
    }
} 